var util = require('../../libs/util.js');
Page({
    data: {
        width: 0,
        company: {},
        banners: [],
        cases: [],
        products: [],
        news: []
    },
    onLoad: function() {
        swan.showNavigationBarLoading();
        this.setData({width: swan.getSystemInfoSync().windowWidth});
        this.init();
    },
    init: function() {
    	var _this = this;
        // 获取轮播
        util.request({
            url: 'article.banner/lists',
            method: 'POST',
            data: {
                limit: 6,
                page: 1
            },
            success: function (res) {
                _this.setData({banners: res.result.data});
            }
        });

    	// 获取新闻数据
        util.request({
            url: 'article.article/lists',
            method: 'POST',
            data: {
                limit: 2,
                page: 1
            },
            success: function (res) {
                _this.setData({news: res.result.data});
            }
        });
        
        // 获取产品
        util.request({
            url: 'goods.goods/lists',
            method: 'POST',
            data: {
                limit: 4,
                page: 1
            },
            success: function (res) {
                _this.setData({products: res.result.data});
            }
        });
        
        // 获取案例
        util.request({
            url: 'article.instance/lists',
            method: 'POST',
            data: {
                limit: 4,
                page: 1
            },
            success: function (res) {
                _this.setData({cases: res.result.data});
            }
        });

        // 获取站点信息
        util.request({
            url: 'site.site/detail',
            method: 'POST',
            data: {},
            success: function (res) {
                if(res.code == 200) {
                    _this.setData({company: res.result});
                    swan.hideNavigationBarLoading();
                    swan.setNavigationBarTitle({title: res.result.name || '首页'});
                }else{
                    swan.showToast({
					    title: res.message,
					    icon: 'none',
					    duration: 1500,
					});
                    swan.setNavigationBarTitle({title: '首页'});
                }
            },
            fail: function() {
                swan.setNavigationBarTitle({title: '首页'});
            }
        });
    },
    bindBannerError: function(e) {
    	var index = e.target.dataset.errorindex;
    	var banners = this.data.banners;
    	banners[index].imgs = "../../images/default.png";
    	this.setData({banners: banners});
    },
    imageLoad: function(e) {
      var ratio = e.detail.width / swan.getSystemInfoSync().windowWidth;
      var height = e.detail.height / ratio;
      var image=this.data.banners;
      //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
      image[e.target.dataset.index]={
         width: swan.getSystemInfoSync().windowWidth,
         height: height
      }
      this.setData({
           images:image
      })
    }
})